from ctypes import *

def setDwordByPoint(valueAddress, value):
    i=0
    while i<4:
        memset(valueAddress+i, value&0xff, 1)
        i+=1
        value>>=8

SECS_TO_100NSECS=10000000
UNIX_TO_WIN=116444736000000000

def fileTimeToUnix(ft):
    t=ft.dwLowDateTime | (ft.dwHighDateTime<<32)
    t-=UNIX_TO_WIN
    t/=SECS_TO_100NSECS
    return max(int(t), 0)


def fileTimeParts(time):
    time*=SECS_TO_100NSECS
    time+=UNIX_TO_WIN
    return time&0xffffffff, time>>32


def setFileTimeByPoint(buf, time):
    low,high=fileTimeParts(time)
    setDwordByPoint(buf, low)
    setDwordByPoint(buf+4, high)


def setLongLongByPoint(valueAddress, value):
    setDwordByPoint(valueAddress, value&0xffffffff)
    setDwordByPoint(valueAddress+4, (value>>32)&0xffffffff)

def setStringByPoint(valueAddress, value, length):
    cnt=0
    for i in value:
        cnt+=2
        if cnt+2>length: break

        memset(valueAddress, ord(i)&0xff, 1)
        valueAddress+=1
        memset(valueAddress, (ord(i)>>8)&0xff, 1)
        valueAddress+=1

    memset(valueAddress, 0, 1)
    valueAddress+=1
    memset(valueAddress, 0, 1)